package com.learn.excel.hawkeye.interceptor;

import cn.hutool.core.net.NetUtil;
import cn.hutool.http.HttpStatus;
import com.learn.excel.core.common.LoginUser;
import com.learn.excel.core.common.annotation.PassToken;
import com.learn.excel.core.common.cache.CacheHandler;
import com.learn.excel.core.consts.ErrorCode;
import com.learn.excel.core.handler.RedisHandle;
import com.learn.excel.core.util.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * Swagger拦截器
 *
 * @author anri
 * @date 2020/10/10 13:54
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Resource
    private CacheHandler cacheHandler;

//    @Resource
//    private RedisHandle redisHandle;

    /**
     * 1. 在所有接口访问后更新所对应 token 的失效时间
     * 2. 增删改操作自动刷新缓存
     * 3. 增删改操作修改数据接收模块的初始状态，使其也
     * 自动刷新缓存
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 从 http 请求头中取出 token
        String token = request.getHeader("token");
        if (null == token) {
            return;
        }

        //设置超时时间为30分钟
//        redisHandle.setUserTokenExpire(token, 60 * 30);
    }

    /**
     * 在所有接口访问前判断是否有 token
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setContentType("application/json; charset=utf-8");
        // 如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        // 检查是否有 PassToken 注释，有则跳过认证
        if (method.isAnnotationPresent(PassToken.class)) {
            PassToken passToken = method.getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }

        // 检查是否是本地host调用接口，是本地则跳过认证
        String ipAddr = IpUtils.getIpAddr();
        if (NetUtil.LOCAL_IP.equals(ipAddr)) {
            return true;
        }
        return true;



    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        cacheHandler.removeUser();
    }

    private boolean onAuthenticateFailed(HttpServletResponse response, String message) throws Exception {
        response.setStatus(HttpStatus.HTTP_UNAUTHORIZED);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(message);
        return false;
    }
}